package com.apes.hr.group.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.group.model.DingDingGroupPerson;
import com.apes.hr.group.repository.DingDingGroupPersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 功能：钉钉建群进群人员
 *
 * @author ZhengZX
 * @create 2020年9月10日10:50:21
 */
@Service("hrDingDingGroupPersonService")
public class DingDingGroupPersonService extends DomainService {

    @Autowired
    DingDingGroupPersonRepository dingDingGroupPersonRepository;

    //保存
    public DingDingGroupPerson save(SimpleRequest request) {
        DingDingGroupPerson dingDingGroupPerson = request.getO(DingDingGroupPerson.class);
        return dingDingGroupPersonRepository.saveAndFlush(dingDingGroupPerson);
    }

    //修改
    public DingDingGroupPerson update(SimpleRequest request) {
        String id = request.get("id");
        DingDingGroupPerson dingDingGroupPerson = request.getO(DingDingGroupPerson.class);
        DingDingGroupPerson tempDingDingGroupPerson = dingDingGroupPersonRepository.findOne(id);
        if (tempDingDingGroupPerson == null) throw new RuntimeException("状态" + id + "】在系统中不存在。");
        if(tempDingDingGroupPerson.getVersion() != dingDingGroupPerson.getVersion()) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        return dingDingGroupPersonRepository.saveAndFlush(dingDingGroupPerson);
    }

    public String delete(SimpleRequest request){
        String id = request.get("id");
        DingDingGroupPerson dingDingGroupPerson = request.getO(DingDingGroupPerson.class);
        DingDingGroupPerson tempDingDingGroupPerson = dingDingGroupPersonRepository.findOne(id);
        if (tempDingDingGroupPerson == null) throw new RuntimeException("状态" + id + "】在系统中不存在。");
        if(tempDingDingGroupPerson.getVersion() != dingDingGroupPerson.getVersion()) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        dingDingGroupPersonRepository.delete(dingDingGroupPerson);
        return "删除成功";
    }
}
